package com.qingzhu.dao.impl;

import com.qingzhu.dao.ReceivingAddressDao;
import com.qingzhu.pojo.ReceivingAddress;
import com.qingzhu.util.CreateSqlSessionFactory;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ReceivingAddressDaoImpl implements ReceivingAddressDao {

    @Override
    public List<ReceivingAddress> findAll(int userId) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ReceivingAddressDao mapper = sqlSession.getMapper(ReceivingAddressDao.class);
            return mapper.findAll(userId);
        }
    }

    @Override
    public ReceivingAddress findById(int id) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ReceivingAddressDao mapper = sqlSession.getMapper(ReceivingAddressDao.class);
            return mapper.findById(id);
        }
    }

    @Override
    public int addOneReceivingAddress(ReceivingAddress receivingAddress) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ReceivingAddressDao mapper = sqlSession.getMapper(ReceivingAddressDao.class);
            return mapper.addOneReceivingAddress(receivingAddress);
        }
    }

    @Override
    public int updateOneReceivingAddress(ReceivingAddress receivingAddress) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ReceivingAddressDao mapper = sqlSession.getMapper(ReceivingAddressDao.class);
            return mapper.updateOneReceivingAddress(receivingAddress);
        }
    }

    @Override
    public int updateDefaultDreceivingAddress(int defaultState, int id) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ReceivingAddressDao mapper = sqlSession.getMapper(ReceivingAddressDao.class);
            return mapper.updateDefaultDreceivingAddress(defaultState,id);
        }
    }

    @Override
    public int delOneReceivingAddress(int id) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            ReceivingAddressDao mapper = sqlSession.getMapper(ReceivingAddressDao.class);
            return mapper.delOneReceivingAddress(id);
        }
    }
}
